package tech.chuangxiang.weichuangxin;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * Created by Great on 2015/10/7.
 */
public class NewslistAdapter extends BaseAdapter {
    private Context ctx;
    private List<Newslist> data;
    private int style;
    private ImageView img;
    private String imgurl = "http://img.managershare.com/uploads/2015/10/63401444550630.jpg";

    //����Ϣ������ʵ�ֶԿؼ��ĸ���
    private Handler handle = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    System.out.println("111");
                    Bitmap bmp=(Bitmap)msg.obj;
                    img.setImageBitmap(bmp);
                    break;
            }
        };
    };
    public NewslistAdapter(Context ctx, List<Newslist> data, int style) {
        super();
        this.ctx = ctx;
        this.data = data;
        this.style = style;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Newslist newslist = data.get(position);
        LayoutInflater layoutInflater = LayoutInflater.from(ctx);


        View v = convertView;

        if (v == null) {
            v = layoutInflater.inflate(R.layout.newslist_row_style, null);
        }

        TextView t1 = (TextView)v.findViewById(R.id.lblName);
        TextView t2 = (TextView)v.findViewById(R.id.lblNote);
        TextView head_text = (TextView)v.findViewById(R.id.head_text);

        String tmp_head = newslist.getTitle();
        String tmp_head1= tmp_head.substring(0, 1);
        String tmp_head2=tmp_head.substring(1);
        head_text.setText(tmp_head1);
        t1.setText(tmp_head2);
        t2.setText(newslist.getBody());
        img = (ImageView)v.findViewById(R.id.img);
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Bitmap bmp = getURLimage(imgurl);
                Message msg = new Message();
                msg.what = 0;
                msg.obj = bmp;
                System.out.println("000");
                handle.sendMessage(msg);
            }
        }).start();
        return v;
    }

    public Bitmap getURLimage(String url) {
        Bitmap bmp = null;
        try {
            URL myurl = new URL(url);
            // �������
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
            conn.setConnectTimeout(6000);//���ó�ʱ
            conn.setDoInput(true);
            conn.setUseCaches(false);//������
            conn.connect();
            InputStream is = conn.getInputStream();//���ͼƬ��������
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;
    }
}
